import request from "@/utils/request";

/**
 * 无人机管理相关API
 */
const droneService = {
  // 获取无人机列表
  getDrones(params) {
    return request({
      url: "/drones",
      method: "get",
      params,
    });
  },

  // 获取无人机详情
  getDroneDetail(id) {
    return request({
      url: `/drones/${id}`,
      method: "get",
    });
  },

  // 创建无人机
  createDrone(data) {
    return request({
      url: "/drones",
      method: "post",
      data,
    });
  },

  // 更新无人机
  updateDrone(id, data) {
    return request({
      url: `/drones/${id}`,
      method: "put",
      data,
    });
  },

  // 删除无人机
  deleteDrone(id) {
    return request({
      url: `/drones/${id}`,
      method: "delete",
    });
  },

  // 更新无人机状态
  updateDroneStatus(id, status) {
    return request({
      url: `/drones/${id}/status`,
      method: "put",
      data: { status },
    });
  },

  // 获取无人机类型选项
  getDroneTypeOptions() {
    return request({
      url: "/drones/type-options",
      method: "get",
    });
  },

  // 连接无人机
  connectDrone(droneId) {
    return request({
      url: `/drone-control/${droneId}/connect`,
      method: "post",
    });
  },

  // 断开无人机连接
  disconnectDrone(droneId) {
    return request({
      url: `/drone-control/${droneId}/disconnect`,
      method: "post",
    });
  },

  // 获取无人机状态
  getDroneControlStatus(droneId) {
    return request({
      url: `/drone-control/${droneId}/status`,
      method: "get",
    });
  },

  // 发送无人机命令
  sendDroneCommand(droneId, command, params = {}) {
    // 确保参数格式正确，特别是方向控制命令
    if (command === "move" && params && !params.direction) {
      // 如果是move命令但没有指定direction，默认为forward
      params = { ...params, direction: "forward" };
    }

    return request({
      url: `/drone-control/${droneId}/command`,
      method: "post",
      params: { command },
      data: params,
    });
  },

  // PX4 任务更新（LOAD/START 等操作）
  updatePx4Mission(data) {
    return request({
      url: "/drone-control/px4/mission_update",
      method: "put",
      data,
    });
  },

  // 获取 PX4 任务状态
  getPx4MissionStatus(droneAlias) {
    return request({
      url: `/drone-control/px4/mission_status/${droneAlias}`,
      method: "get",
    });
  },

  // # 获取特定类型数据
  // GET /api/px4/pose/uav_1?telemetry_type=battery
  // GET /api/px4/pose/uav_1?telemetry_type=gps

  // # 获取批量数据
  // GET /api/px4/pose/uav_1?telemetry_type=batch
  // 获取 PX4 姿态信息
  getPx4Pose(droneAlias) {
    return request({
      url: `/drone-control/px4/pose/${droneAlias}?telemetry_type=all`,
      method: "get",
    });
  },

  // 获取当前所有无人机连接列表
  getConnectionsList() {
    return request({
      url: "/drone-control/connections",
      method: "get",
    });
  },

  /**
   * 更新无人机状态（用于实时更新）
   * @param {number} droneId 无人机ID
   * @param {Object} status 更新的状态信息
   * @returns {Promise}
   */
  updateDroneControlStatus(droneId, status) {
    return request({
      url: `/drone-control/${droneId}/status`,
      method: "put",
      data: status,
    });
  },
};

export default droneService;
